import sys
import os
import django

# 添加后端路径
sys.path.append('../backend')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'massage_app.settings')

# 初始化Django
django.setup()

from services.models import Banner, Category, Technician, Service

def save_images_to_database():
    """将爬取的图片链接保存到数据库"""
    print("开始将图片链接保存到数据库...")
    
    # 清空现有数据
    Banner.objects.all().delete()
    Technician.objects.all().delete()
    Service.objects.all().delete()
    
    # 创建轮播图数据
    Banner.objects.create(
        title="秋季养生特惠",
        description="专业按摩服务，享受健康生活",
        image="http://minapi.huaxianguan.com/anmo/banners/banner1.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081820Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=ed0e86311f88d702a8eebcac5bc4fec45b36870924c2f770649a9033ee321620",
        sort_order=1
    )
    
    Banner.objects.create(
        title="新用户专享",
        description="首次预约立减50元",
        image="http://minapi.huaxianguan.com/anmo/banners/banner2.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081825Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=950d3b8c141abcf31f44f175ba7be11f84493848be6479171420ff77f00909d4",
        sort_order=2
    )
    
    # 更新分类图标
    try:
        massage_category = Category.objects.get(name="massage")
        massage_category.icon = "http://minapi.huaxianguan.com/anmo/icons/massage.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081833Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=7325619222012213f6f2dbdb91ccd0130d3f9837816fe371e6817e3e0d56db9c"
        massage_category.save()
        
        spa_category = Category.objects.get(name="spa")
        spa_category.icon = "http://minapi.huaxianguan.com/anmo/icons/spa.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T074432Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=5bff947f29171d0f92336eef060359a5b3166801d7c6cc3d6ac7f1857c13198c"
        spa_category.save()
        
        footcare_category = Category.objects.get(name="footcare")
        footcare_category.icon = "http://minapi.huaxianguan.com/anmo/icons/footcare.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T074432Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=2055194e78865dd8ee81a73c690613083ef1cbb0cbcba27d0b00fce1e52ef50c"
        footcare_category.save()
        
        bodycare_category = Category.objects.get(name="bodycare")
        bodycare_category.icon = "http://minapi.huaxianguan.com/anmo/icons/bodycare.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081904Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=f0d2929e77a1f524b1ee215f76afa4659d9489b8d6f26770ad84f67d2e7fb915"
        bodycare_category.save()
    except Category.DoesNotExist:
        print("分类不存在，创建新的分类...")
        Category.objects.create(
            name="massage",
            icon="http://minapi.huaxianguan.com/anmo/icons/massage.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081833Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=7325619222012213f6f2dbdb91ccd0130d3f9837816fe371e6817e3e0d56db9c",
            sort_order=1
        )
        Category.objects.create(
            name="spa",
            icon="http://minapi.huaxianguan.com/anmo/icons/spa.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T074432Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=5bff947f29171d0f92336eef060359a5b3166801d7c6cc3d6ac7f1857c13198c",
            sort_order=2
        )
        Category.objects.create(
            name="footcare",
            icon="http://minapi.huaxianguan.com/anmo/icons/footcare.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T074432Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=2055194e78865dd8ee81a73c690613083ef1cbb0cbcba27d0b00fce1e52ef50c",
            sort_order=3
        )
        Category.objects.create(
            name="bodycare",
            icon="http://minapi.huaxianguan.com/anmo/icons/bodycare.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081904Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=f0d2929e77a1f524b1ee215f76afa4659d9489b8d6f26770ad84f67d2e7fb915",
            sort_order=4
        )
    
    # 创建技师数据
    Technician.objects.create(
        name="张技师",
        avatar="http://minapi.huaxianguan.com/anmo/avatars/technician1.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081910Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=664660f9675ae02a8bc2bf4acac3a0a68a226504e7c677de6e64d41dbe6c17e1",
        rating=4.8,
        order_count=128
    )
    
    Technician.objects.create(
        name="李技师",
        avatar="http://minapi.huaxianguan.com/anmo/avatars/technician2.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081915Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=a2d0cf3205f5ba39d543c776d62be45d106ec8cee3d3ed6dba1fa9393e90d3f3",
        rating=4.9,
        order_count=96
    )
    
    Technician.objects.create(
        name="王技师",
        avatar="http://minapi.huaxianguan.com/anmo/avatars/technician3.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081920Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=65a08e6dca328ee2dcd317900797f5e119db74fcd6cc5146fcc9b483ef81df79",
        rating=4.7,
        order_count=142
    )
    
    # 创建服务数据
    Service.objects.create(
        name="全身放松按摩",
        category="massage",
        description="专业的全身放松按摩服务，缓解疲劳",
        duration=60,
        price=198.00,
        image="http://minapi.huaxianguan.com/anmo/services/service1.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081926Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=74230c7762a4acb7a076cb92aeb5b9ea19a2153f8990c55ed09f31dba58a04a4"
    )
    
    Service.objects.create(
        name="足底按摩",
        category="footcare",
        description="专业的足底按摩服务，促进血液循环",
        duration=45,
        price=128.00,
        image="http://minapi.huaxianguan.com/anmo/services/service2.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=admin%2F20251017%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20251017T081931Z&X-Amz-Expires=3600&X-Amz-SignedHeaders=host&X-Amz-Signature=5443cf813ebd515b92d3282ff81745432c27fd36cea2e3ae68659fc95ffa21c2"
    )
    
    print("所有图片链接已保存到数据库")

if __name__ == "__main__":
    save_images_to_database()